﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using pc_system.Model;
using PosTest;

namespace pc_system.View
{
    public partial class saleFinal : Form
    {
        private float ftotal = 0.0f;
        private float shou = 0.0f;
        private float changes = 0.0f;
        public float Ftotal
        {
            get { return this.ftotal;}
        }

        public float Shou
        {
            get { return this.shou; }
        }
        public float Changes
        {
            get { return this.changes; }
        }

        private string paykind;
        public string PayKind
        {
            get { return paykind; }
        }
        CustomPos pos = new CustomPos(GlobalVariables.POS_PORT);

        public saleFinal(float total)
        {
            InitializeComponent();
            ftotal = total;
        }




        void sale_total_shoukuan_TextChanged(object sender, EventArgs e)
        {
            if (!float.TryParse(sale_total_shoukuan.Text,out shou)){
                MessageBox.Show("请重新输入");
                sale_total_shoukuan.Focus();
                sale_total_shoukuan.SelectAll();
            }
            changes = shou - ftotal;
            sale_total_change.Text = (shou - ftotal).ToString("0.00");
            //TODO:客显 对应不同收款
            pos.SetDisplayMode(CustomPos.DisplayMode.Balance);
            pos.Display(sale_total_change.Text);
        }


        private void textbox_MouseClick(object sender, MouseEventArgs e)
        {
            TextBox tb = (TextBox)sender;
            tb.SelectAll();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            button1.BackColor = Color.Brown;
            button1.ForeColor = Color.White;
            button2.BackColor = Color.FromArgb(241, 229, 216);
            button2.ForeColor = Color.DimGray;
            button3.BackColor = Color.FromArgb(241, 229, 216);
            button3.ForeColor = Color.DimGray;
            this.paykind = "现金";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            button2.BackColor = Color.Brown;
            button2.ForeColor = Color.White;
            button1.BackColor = Color.FromArgb(241, 229, 216);
            button1.ForeColor = Color.DimGray;
            button3.BackColor = Color.FromArgb(241, 229, 216);
            button3.ForeColor = Color.DimGray;
            this.paykind = "会员卡";
        }

        private void button3_Click(object sender, EventArgs e)
        {
            button3.BackColor = Color.Brown;
            button3.ForeColor = Color.White;
            button1.BackColor = Color.FromArgb(241, 229, 216);
            button2.ForeColor = Color.DimGray;
            button2.BackColor = Color.FromArgb(241, 229, 216);
            button1.ForeColor = Color.DimGray;
            this.paykind = "刷卡";
        }

        private void saleFinal_Load(object sender, EventArgs e)
        {
            this.salefinal_title.Location = new System.Drawing.Point(System.Convert.ToInt32(this.Width - this.salefinal_title.Width) / 2,
     System.Convert.ToInt32(this.salefinal_title.Height) / 2);
            sale_total_shoukuan.TextChanged += sale_total_shoukuan_TextChanged;
            sale_total.Text = ftotal.ToString("0.00");
            sale_total_shoukuan.Text = ftotal.ToString("0.00");
            sale_total.Enabled = false;
            sale_total_shoukuan.Focus();
            sale_total_shoukuan.SelectAll();
            button1.PerformClick();
            sale_comfirm.Click += sale_comfirm_Click;
        }

        void sale_comfirm_Click(object sender, EventArgs e)
        {
            //TODO: 打印订单
            if (changes < 0)
            {
                sale_total_shoukuan.Text = "";
                return;
            }
            MessageBox.Show("打印中。。。。。。。。。。");
            pos.Init();
            this.DialogResult = DialogResult.OK;
        }

        #region 退出快捷键
        //重写处理命令键方法以监听快捷键
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {   
            

            if (keyData == Keys.Escape)
            {
                //System.Windows.Forms.SendKeys.Send("{F5}");
                this.Close();
                return true;
            }
            if (keyData == Keys.Enter)
            {
                //System.Windows.Forms.SendKeys.Send("{F5}");
                this.sale_comfirm.PerformClick();
                return true;
            }

            //继续原来base.ProcessCmdKey中的处理
            return base.ProcessCmdKey(ref msg, keyData);
        }


        #endregion

      
    }
}
